package io.sk.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.sk.common.base.BaseResult;
import io.sk.common.base.Constants;
import io.sk.common.utils.CheckDataUtils;
import io.sk.common.utils.ResourceUtils;
import io.sk.dao.ClassDao;
import io.sk.dto.ClassAlbumDto;
import io.sk.entity.AlbumEntity;
import io.sk.entity.ClassEntity;
import io.sk.service.AlbumService;
import io.sk.service.ClassService;
import io.sk.service.ClassTeacherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 班级信息表
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/8 16:37
 */
@Service("classService")
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassEntity> implements ClassService {

    @Autowired
    private ClassDao classDao;

    @Autowired
    private ClassTeacherService classTeacherService;

    @Autowired
    private AlbumService albumService;

    /**
     * 创建班级
     *
     * @param classEn 班级信息
     * @return BaseResult
     */
    @Override
    public BaseResult save(ClassEntity classEn) {

        //数据验证
        if (classEn == null) {
            return CheckDataUtils.sendResult400();
        }

        //班级参数验证
        BaseResult baseResult = checkClass(classEn);
        if (Constants.RESPONSE_CODE_200 != baseResult.getCode()) {
            return baseResult;
        }
        classEn = (ClassEntity) baseResult.getData();

        classEn.preInsert(classEn.getTeacherId());

        //班级创建状态：1、待审核，2、审核通过，3、审核拒绝，4、已解散
        classEn.setStatus(Constants.CLASS_STATUS_PENDING);
        classEn.setPhotoTatal(Constants.INT_ZERO);

        Integer updateNum = classDao.insert(classEn);
        if (updateNum < Constants.INT_ONE) {
            return CheckDataUtils.sendResult605("班级保存失败");
        }

        //新建人员班级关系信息
        boolean bl = classTeacherService.save(
                classEn.getId(),
                classEn.getTeacherId(),
                null,
                Constants.STRING_BLANK
        );
        if (!bl) {
            return CheckDataUtils.sendResult605("班级保存失败");
        }

        //默认相册创建
        AlbumEntity album = new AlbumEntity();
        album.setIsDefault(Constants.DEFAULT_YES);
        album.setClassId(classEn.getId());
        baseResult = albumService.save(album, classEn.getTeacherId());
        if (Constants.RESPONSE_CODE_200 != baseResult.getCode()) {
            return baseResult;
        }

        return CheckDataUtils.sendResult200();
    }

    /**
     * 班级下拉列表查询
     *
     * @param searchValue 查询条件：班级群号、名称（幼儿园名称+班级名称）、老师名称
     * @return BaseResult
     */
    @Override
    public BaseResult selectList(String searchValue) {

        //数据验在
        searchValue = CheckDataUtils.stringTrim(searchValue);

        return CheckDataUtils.sendResult200(classDao.selectList(searchValue));
    }

    /**
     * 相册管理——班级列表查询
     *
     * @param id 教师id
     * @return BaseResult
     */
    @Override
    public BaseResult albumClassList(String id) {

        return CheckDataUtils.sendResult200(classDao.albumClassList(id));
    }

    /**
     * 班级参数验证
     *
     * @param classEn 班级信息
     * @return BaseResult
     */
    private BaseResult checkClass(ClassEntity classEn) {

        //中间参数
        String middleStr;

        //群id
        classEn.setGroupId(ResourceUtils.getUUID());

        //所属幼儿园id
        middleStr = CheckDataUtils.stringTrim(classEn.getNurseryId());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到所属幼儿园");
        }
        classEn.setNurseryId(middleStr);

        //班级名称:12个汉字内
        middleStr = CheckDataUtils.stringTrim(classEn.getName());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到班级名称");
        }
        classEn.setName(middleStr);

        //班主任id
        middleStr = CheckDataUtils.stringTrim(classEn.getTeacherId());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到班主任主键");
        }
        classEn.setTeacherId(middleStr);

        //班主任名称
//        middleStr = CheckDataUtils.stringTrim(classEn.getTeacherName());
//        if (StringUtils.isEmpty(middleStr)){
//            return CheckDataUtils.sendResult400("未找到班主任名称");
//        }
//        classEn.setTeacherName(middleStr);
        classEn.setTeacherName(CheckDataUtils.stringTrim(classEn.getTeacherName()));

        return CheckDataUtils.sendResult200(classEn);
    }
}
